package com.powerunion.datacollection.service;

import java.util.Date;
import net.sf.json.JSONObject;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import com.powerunion.datacollection.common.Format;
import com.powerunion.datacollection.common.SystemException;
import com.powerunion.datacollection.dao.DaoFactory;
import com.powerunion.datacollection.dao.DataCollectionFormDao;
import com.powerunion.datacollection.model.DataCollectionForm;
import com.powerunion.datacollection.model.jsonObject.DataCollectionFormJSONObject;
import com.powerunion.datacollection.model.query.DataCollectionFormQuery;
import com.powerunion.util.log.LogHelper;

public class DataCollectionFormManager {
	private static Logger logger = LogHelper.getInstance().getLogger(
			DataCollectionFormManager.class);
	private DataCollectionFormDao dao = null;

	public DataCollectionFormManager() {
		super();
		this.dao = (DataCollectionFormDao) DaoFactory.getInstance().getDao(
				DataCollectionFormDao.class);
	}

	public String save(String saveStr, String currentUser)  throws Exception {
		// 参数为空
		if (StringUtils.isEmpty(saveStr)) {
			String[] args = { "saveStr" };
			// 打印错误日志
			StringBuffer msglog = new StringBuffer(
					"empty invoke parameter saveStr NULL ");
			logger.error(msglog.toString());
			throw new SystemException("error.common.parameter.empty", args);
		}

		JSONObject jsonObject = JSONObject.fromObject(saveStr);
		DataCollectionFormJSONObject dcfjo = (DataCollectionFormJSONObject) JSONObject.toBean(
				jsonObject, DataCollectionFormJSONObject.class);
		
		DataCollectionForm dcf = new DataCollectionForm();

		//复制属性值
	    BeanUtils.copyProperties(dcf,dcfjo);

	    String createDateStr = dcfjo.getCreateDateStr();
	    Date createDate = Format.getInstance().stringToDate(createDateStr, "yyyy-MM-dd");
	    dcf.setCreateDate(createDate);
	    
	    if(dcf.getId().equals("")) {
	    	 dcf.setCreateUser(currentUser);
	    	 
	    }
	    dcf.setModifyUser(currentUser);
	    dcf.setModifyDate(new Date());
	    
		return this.dao.save(dcf);

	}

	public boolean delete(String[] codes) throws Exception{
		// 参数为空
		if (codes.length < 1) {
			String[] args = { "codes" };
			// 打印错误日志
			StringBuffer msglog = new StringBuffer(
					"empty invoke parameter codes NULL ");
			logger.error(msglog.toString());
			throw new SystemException("error.common.parameter.empty", args);
		}

		return this.dao.delete(codes);
	}

	public DataCollectionForm findByKey(String id) throws Exception {
		// 参数为空
		if (StringUtils.isEmpty(id)) {
			String[] args = { "id" };
			// 打印错误日志
			StringBuffer msglog = new StringBuffer(
					"empty invoke parameter id NULL ");
			logger.error(msglog.toString());
			throw new SystemException("error.common.parameter.empty", args);
		}

		return this.dao.getBykey(id);
	}

	public String getDataCollectionForms(DataCollectionFormQuery query,
			int start, int limit) {
		// 参数为空
		if (query == null) {
			String[] args = { "query" };
			// 打印错误日志
			StringBuffer msglog = new StringBuffer(
					"empty invoke parameter query NULL ");
			logger.error(msglog.toString());
			throw new SystemException("error.common.parameter.empty", args);
		}
		String result = this.dao.getDataCollectionForms(query, start, limit);
		return result;
	}

	public String loadInfo(String id) throws Exception {
//		// 参数为空
//		if (StringUtils.isEmpty(id)) {
//			String[] args = { "id" };
//			// 打印错误日志
//			StringBuffer msglog = new StringBuffer(
//					"empty invoke parameter id NULL ");
//			logger.error(msglog.toString());
//			throw new SystemException("error.common.parameter.empty", args);
//		}
//
//		DataCollectionForm dcf = this.dao.getBykey(id);
//
//		FormFieldManager ffm = new FormFieldManager();
//		List principalFormFields = ffm.getFormFieldsByFormId(id, "0");
//		List subordinateFormFields = ffm.getFormFieldsByFormId(id, "1");
//
//		dcf.setPrincipalTableFields(principalFormFields);
//		dcf.setSubordinateTableFields(subordinateFormFields);
//
//		JSONObject jsonObject = JSONObject.fromObject(dcf);
//
//		String info = "{success:true,data:[" + jsonObject + "]}";
//
//		return info;
		return "";

	}

	public String getUploadFiles(int start, int limit, String actId,String formId) {
		// 参数为空
		if (StringUtils.isEmpty(formId)) {
			String[] args = { "formId" };
			// 打印错误日志
			StringBuffer msglog = new StringBuffer(
					"empty invoke parameter formId NULL ");
			logger.error(msglog.toString());
			throw new SystemException("error.common.parameter.empty", args);
		}

		String results = "";
		results = this.dao.getUploadFiles(start, limit, actId, formId);
		return results;

	}

	public String deleteUploadFiles(String path, String[] codes) {
		// 参数为空
		if (StringUtils.isEmpty(path)) {
			String[] args = { "path" };
			// 打印错误日志
			StringBuffer msglog = new StringBuffer(
					"empty invoke parameter filePath NULL ");
			logger.error(msglog.toString());
			throw new SystemException("error.common.parameter.empty", args);
		}

		// 参数为空
		if (codes.length < 1) {
			String[] args = { "codes" };
			// 打印错误日志
			StringBuffer msglog = new StringBuffer(
					"empty invoke parameter codes NULL ");
			logger.error(msglog.toString());
			throw new SystemException("error.common.parameter.empty", args);
		}

		return this.dao.deleteUploadFiles(path, codes);
	}
	
}
